window.onload = function() {
	var autoindex = {
		ver: '1.1',
		release: '20190624',
		author: 'Jaylan',
		email: '755412627@qq.com',
		db: {
			prefix: 'autoindex_',
			get: function(key){
				if(key)key = this.prefix + key;
				return localStorage.getItem(key);
			},
			set: function(key, value){
				if(key)key = this.prefix + key;
				localStorage.setItem(key, value);
			},
			remove: function(key){
				if(key)key = this.prefix + key;
				localStorage.removeItem(key);
			},
			clear: function(){
				localStorage.clear();
			}
		},
		target: '',
		toggle: function(action){
			switch(action){
				case 'target':
					if(this.target){
						autoindex.db.remove('target');
					}else{
						autoindex.db.set('target', '_blank');
					};
					location.reload();
					break;
				case 'reset':
					autoindex.db.clear();
					location.reload();
					break;
				default:
					break;
			};
		},
		init: function(){
			if(window.localStorage){
				this.target = this.db.get('target');
			};
			if(typeof(document.querySelector)=='function'){
				var autoindexDoctype = document.implementation.createDocumentType('html', '', '');
				if(document.doctype){
					document.replaceChild(autoindexDoctype, document.doctype);
				}else{
					document.insertBefore(autoindexDoctype, document.childNodes[0]);
				};
				var autoindexList = document.querySelector('body>pre');
				if(autoindexList){
					var head = document.querySelector('head#autoindex-head');
					if(!head){
						head = document.head;
						var meta = document.createElement('meta');
						meta.setAttribute('charset', 'utf-8');
						head.appendChild(meta);
						meta = document.createElement('meta');
						meta.setAttribute('name', 'viewport');
						meta.setAttribute('content', 'width=device-width,initial-scale=1');
						head.appendChild(meta);
						meta = document.createElement('meta');
						meta.setAttribute('http-equiv', 'X-UA-Compatible');
						meta.setAttribute('content', 'IE=edge,chrome=1');
						head.appendChild(meta);
						meta = document.createElement('meta');
						meta.setAttribute('name', 'author');
						meta.setAttribute('content', 'Jack.Chan');
						head.appendChild(meta);
					};
					var listTag = 'ol';
					var html = [], item = '', itemArr = [], itemLink = '', itemInfo = '', itemExternal = '';
					var dataArr = autoindexList.innerHTML.split('\n');
					for(var i in dataArr){
						item = String(dataArr[i]).trim();
						if(item){
							if(item.indexOf('</a>') > -1){
								itemArr = item.split('</a>');
								itemLink = String(itemArr[0]).trim();
								if(i>0){
									if(autoindex.target){
										itemLink = itemLink.replace('<a', '<a target="'+ autoindex.target +'"') + '</a>';
									}else{
										itemExternal = itemLink.replace('<a', '<a class="external" target="_blank" title="external"').replace('">', '&down=1">') + '</a>';
									};
								};
								itemInfo = String(itemArr[1]).trim();
								item = itemLink + itemExternal + (itemInfo ? '<span>'+ itemInfo +'</span>' : '');
							};
							html.push('<li'+ (i==0 ? ' class="parent"' : '') +'>'+ item +'</li>');
							itemArr = [];
							item = itemLink = itemInfo = itemExternal = '';
						};
					};
					if(html.length){
						html.unshift('<'+ listTag +' class="autoindex-list">');
						html.push('</'+ listTag +'>');
						// 编辑上传页面链接
						var uri = document.baseURI.split(document.domain)[1];
						uri = uri.substring(uri.indexOf("/"), uri.length)
						if (uri.indexOf('?') > -1) {
						    uri = uri.substring(0, uri.indexOf("?"))
						}
						while (uri.indexOf("//") >= 0) {
						    uri.replace("//", "/");
						}
						remote_dir = uri.replace("/189", "");
						html.push('<p class="autoindex-copyright"><span id="autoindex-toggle" class="autoindex-toggle'+ (autoindex.target ? ' active' :'') +'">autoindex</span> for Nginx.&nbsp;&copy;&nbsp;<a target="_blank" href="/189/offine_download.html?remote_dir=' + remote_dir + '">离线下载</a>&nbsp;&copy;&nbsp;<a target="_blank" href="/189/upload.html?remote_dir=' + remote_dir + '" >上传文件</p>');
						autoindexList.innerHTML = html.join('');
						var autoindexToggle = document.querySelector('#autoindex-toggle');
						if(autoindexToggle){
							autoindexToggle.onclick = function(){
								autoindex.toggle('target');
							};
						};
					};
					html = item = itemArr = itemLink = itemInfo = '';
				};
			}else{
				var autoindexUnsupported = 'autoindex ->> your browser didn\'t support : " document.querySelector "';
				if(window.console){
					console.error(autoindexUnsupported);
				}else{
					document.write('<p class="autoindex-unsupported">'+ autoindexUnsupported +'</p>');
				};
			};
		}
	};

	autoindex.init();
	window.autoindex = autoindex;
}